<?php
if (!isset($model))
    $model = new BDynamicFormModel ();
$productCodeField = 'media_productCode_product_info';
$productNameField = 'media_name_product_info';
$productDescriptionField = 'media_description_product_info';
$model->addField($productNameField);
$model->addField($productDescriptionField);
$model->addField($productCodeField);

$attributeNameField = 'media_name_product_attribute';
$attributeAliasField = 'media_alias_product_attribute';
$attributeTypeField = 'media_type_product_attribute';
$model->addField($attributeNameField);
$model->addField($attributeAliasField);
$model->addField($attributeTypeField);

$productAttributeView = array();

if ($productId > 0) {
    $productTable = new BPProductSheet();
    $product = $productTable->getListById($productId);

    $productAttributeTable = new BPProductSheet('productAttribute', $product);
    $productAttributeView = $productAttributeTable->getView();

    /**
     * below code will populate attribute for product
     */
    foreach ($productAttributeView as $id => $productAttribute) {
        $nameItem = $productAttribute->getItem('name');
        $typeItem = $productAttribute->getItem('type');
        if ($nameItem instanceof BTreeItem) {
            $name = $nameItem->getValue();
            $attributeField = 'media_' . $name . '_product_info';
            $model->addField($attributeField);
            //get value of attribute in product 
            $productAttributeItem = $product->getItem($name);
            if ($productAttributeItem instanceof BTreeItem) {
                $attributeValue = $productAttributeItem->getValue();
                $model->$attributeField = $attributeValue;
            }
            else
                $model->$attributeField = '';
        }
        if ($typeItem instanceof BTreeItem) {
//            $type = $nameItem->getValue();
//            $attributeField = 'media_' . $type . '_product_info';
            $type = $typeItem->getValue();
            if($type == 'image')
                $attributeField = 'media_' . $type . '_product_info';
            $model->addField($attributeField);
            $attributeItem = $product->getItem($type);
            if ($attributeItem instanceof BTreeItem) {
                $attributeValue = $attributeItem->getValue();
                $model->$attributeField = $attributeValue;
            }
        }
    }

    $productCodeItem = $product->getItem('productCode');
    if ($productCodeItem instanceof BTreeItem) {
        $productCode = $productCodeItem->getValue();
        $model->$productCodeField = $productCode;
    }
    $productNameItem = $product->getItem('name');
    if ($productNameItem instanceof BTreeItem) {
        $productName = $productNameItem->getValue();
        $model->$productNameField = $productName;
    }
    $productDescriptionItem = $product->getItem('description');
    if ($productDescriptionItem instanceof BTreeItem) {
        $productDescription = $productDescriptionItem->getValue();
        $model->$productDescriptionField = $productDescription;
    }
}
?>
<?php
$app = Blyn::app()->getActiveApp();
?>
<div  class="well-small row-fluid">
    <div class="input-prepend span3">
        <span class="add-on">属性名</span>
        <?php
        echo CHtml::activeTextField($model, $attributeNameField, array('placeholder' => 'attribute' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    <div class="input-prepend span3">
        <span class="add-on">别名</span>
        <?php
        echo CHtml::activeTextField($model, $attributeAliasField, array('placeholder' => 'alias' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    <div class="input-prepend span3">
        <span class="add-on">类型</span>
        <?php
        $typeList = $app->getDataTypeList();
        echo CHtml::activeDropDownList($model, $attributeTypeField, $typeList, array('required' => true, 'class' => 'span8'));
        ?>
    </div>
    <div class="input-append span2">
        <?php
//        echo $control->getAjaxLink(array('name' => '设置数据', 'id' => 'setDataTypeType'), array(
//            'htmlOptions' => array('class' => 'btn'),
//            'productId' => $productId,
//            'attributeId' => 0
//        ));
        ?>
        <?php
        echo $control->getAjaxLink(array('name' => '添加属性', 'id' => 'addProductAttribute'), array(
            'htmlOptions' => array('class' => 'btn'),
            'productId' => $productId,
            'eventId' => $eventId,
            'attributeId' => 0
        ));
        ?>
    </div>

</div>
<!--<div class="row-fluid">
    <div class="span2 offset9">
        <?php
//        echo $control->getAjaxLink(array('name' => '刷新属性', 'id' => 'product_add_info_refresh'), array(
//            'productId' => $productId
//        ));
        ?>
    </div>
</div>-->
<table class="table table-hover">
    <colgroup>
        <col class="span2">
        <col class="span6">
        <col class="span2">
    </colgroup>
    <thead>
        <tr>
            <th><?php echo '属性'; ?></th>
            <th><?php echo '内容'; ?></th>
            <th><?php echo '类型'; ?></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <?php echo '产品编号'; ?>
            </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $productCodeField, array('placeholder' => '产品编号' . '...', 'required' => true));
                ?>
                <!--                    &nbsp;&nbsp;&nbsp;&nbsp;<a id="CreateNewType_clink" role="button" data-toggle="modal" class="btn disabled" style="font-size: 12px;margin-top: -12px;">创建新类型</a>-->
            </td>
            <td>String</td>
        </tr>
        <tr><td> <?php echo '产品名称'; ?> </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $productNameField, array('placeholder' => '产品名称' . '...', 'required' => true));
                ?>
            </td>
            <td>String</td>
        </tr>               
        <tr>
            <td> <?php echo '产品简介'; ?>  </td>                    
            <td>
                <?php
                echo CHtml::activeTextArea($model, $productDescriptionField, array('placeholder' => '产品简介' . '...'));
                ?>
            </td>
            <td><div class='center-block'>String</div></td>
        </tr>
        <?php
        if ($productId > 0) {
            foreach ($productAttributeView as $productAttribute) {
                ?>
                <tr>
                    <?php
                    $nameItem = $productAttribute->getItem('name');
                    $typeItem = $productAttribute->getItem('type');
                    $aliasItem = $productAttribute->getItem('alias');
                    $name = '';
                    $type = '';
                    $alias = '';
                    $attributeValue = '';
                    if ($nameItem instanceof BTreeItem) {
                        $name = $nameItem->getValue();
                        $attributeField = 'media_' . $name . '_product_info';
                        $productAttributeItem = $product->getItem($name);
                        if ($productAttributeItem instanceof BTreeItem) {
                            $attributeValue = $productAttributeItem->getValue();
                            $model->$attributeField = $attributeValue;
                        }
                        else
                            $model->$attributeField = '';
                    }
                    if ($typeItem instanceof BTreeItem) {
                        $type = $typeItem->getValue();
                        //get typeData from define in attribute
                        $typeData = $app->getModelDataByDataType($typeItem);
                    }
                    if ($aliasItem instanceof BTreeItem) {
                        $alias = $aliasItem->getValue();
                    }
                    if ($alias == '')
                        $alias = $name;
                    ?>
                    <td> <?php echo $alias; ?>  </td>   
                    <td> <?php
                        $modelData = array();
                        $modelData['model'] = $model;
                        $modelData['field'] = $attributeField;
                        $modelData['data'] = $typeData;
                        if (isset($form))
                            $modelData['form'] = $form;
                        echo $app->showActiveFieldByDataType($type, $modelData);
                        ?>  </td>
                    <td> <?php echo $type; ?>  </td>
                </tr>
                <?php
            }
        }
        ?>
    </tbody>
</table>

